import 'package:flutter_taotaoshipin/http/request/order_apple_receipt_request.dart';
import 'package:flutter_taotaoshipin/http/request/order_create_request.dart';
import 'package:flutter_taotaoshipin/http/request/order_query_request.dart';
import 'package:flutter_taotaoshipin/model/base_entity.dart';
import 'package:flutter_taotaoshipin/model/order_apple_receipt_entity.dart';
import 'package:flutter_taotaoshipin/model/order_create_entity.dart';
import 'package:flutter_taotaoshipin/model/order_query_entity.dart';
import 'package:flutter_taotaoshipin/util/device_utils.dart';
import 'package:flutter_taotaoshipin/value/constant.dart';
import 'package:hi_net/hi_net.dart';

class OrderDao {
  /// 支付下单
  static Future<BaseEntity<OrderCreateEntity>> create(
    double amount,
    int goodsId,
    String payCode,
    String payAccount, {
    int dramaAction = 1,
    int? payPlatform,
  }) async {
    var request = OrderCreateRequest();
    request.add('amount', amount);
    request.add('goods_id', goodsId);
    request.add('pudid', Device.udid());
    request.add('pfrom', Constant.from);
    request.add('pay_code', payCode);
    request.add('pay_account', payAccount);
    request.add('drama_action', dramaAction);
    if (payPlatform != null) {
      request.add('pay_platform', payPlatform);
    }
    var result = await HiNet.getInstance().fire(request);
    return BaseEntity<OrderCreateEntity>.fromJson(result);
  }

  /// 订单状态主动查询
  static Future<BaseEntity<OrderQueryEntity>> query(String orderId) async {
    var request = OrderQueryRequest();
    request.add('order_id', orderId);
    var result = await HiNet.getInstance().fire(request);
    return BaseEntity<OrderQueryEntity>.fromJson(result);
  }

  /// 苹果支付票据验证
  static Future<BaseEntity<OrderAppleReceiptEntity>> appleReceipt(
    String receipt_data,
    String transaction_id, {
    String orderId = '',
    String uuid = '',
  }) async {
    var request = OrderAppleReceiptRequest();
    request.add('order_id', orderId);
    request.add('uuid', uuid);
    request.add('receipt_data', receipt_data);
    request.add('transaction_id', transaction_id);
    var result = await HiNet.getInstance().fire(request);
    return BaseEntity<OrderAppleReceiptEntity>.fromJson(result);
  }
}
